#@layout("频道订阅") 

#define main()
<fieldset class="layui-elem-field">
	<legend>频道订阅</legend>
	<div style="width: 120em; overflow: auto;">
		<div class="layui-field-box layui-form">
			<table class="layui-table admin-table">
				<thead>
					<tr>
						<th style="width: 30px;"><input type="checkbox"
							lay-filter="allselector" lay-skin="primary"></th>
						<th>频道序号</th>
						<th>频道名</th>
						<th>作者</th>
						<th>发布时间</th>
						<th>URL</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody id="content">
				</tbody>
				<tfoot>
					<tr>
						<th colspan="7 ">
							<div class="admin-table-page">
								<div id="paged" class="page"></div>
							</div>
						</th>
					</tr>
				</tfoot>
			</table>
		</div>
	</div>
</fieldset>
#end 

#define searchForm()
			<blockquote class="layui-elem-quote">
				<a href="javascript:;" data-opt="add" class="layui-btn">添加</a>
			</blockquote>
#end

#define js()
<!--模板-->
<script type="text/html" id="tpl">
			{{# layui.each(d.list.list, function(index, item){ }}
			<tr>
				<td><input type="checkbox" lay-skin="primary"></td>
				<td>{{ item.id}}</td>
				<td>{{ item.name }}</td>
				<td>{{ item.cuser }}</td>
				<td>{{ item.ctime }}</td>
				<td>#(session.basepath ?? ){{ item.url }}</td>
				<td><a href="javascript:;" data-name='{{ item.id }}' data-opt="edit" class="layui-btn layui-btn-mini">订阅</a></td>
			</tr>
			{{# }); }}
		</script>
<script type="text/javascript">

			layui.config({
				base: 'js/'
			});
			layui.use(['paging', 'form', 'laydate', 'upload'], function() {
			var $ = layui.jquery,
				paging = layui.paging(),
				layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
				layer = layui.layer, //获取当前窗口的layer对象
				form = layui.form();
			//新增
			$('a[data-opt=add]').on('click', function() {
				
				$.get('htmlTemp/addChanel.html', null, function(form) {
					addBoxIndex = layer.open({
						type: 1,
						title: '添加频道',
						content: form,
						btn: ['确定', '取消'],
						shade: false,
						offset: ['100px', '30%'],
						area: ['400px', '200px'],
						zIndex: 19950924,
						yes: function(index) {
							$('form.layui-form').find('button[lay-filter=edit]').click();
						},
						success: function(layero, index) {

							var form = layui.form();

							form.on('submit(edit)', function(data) {
								 $.ajax({
										type: 'post',
										url:  'cha/add',
										async: false,
										data: data.field,
										dataType: 'json',
										success: function(ret, status, xhr) {
											if(ret.isOk){
												layerTips.msg(ret.msg);
												location.reload();
											}else{
												layerTips.msg(ret.msg);
											}
											layer.close(index);
									}
							     });
								
							});
							}
						});
					});
				
				});

			var data = {};
			var loadtab = function(data){ 
			paging.init({
				url: 'cha/channels',  //地址
				elem: '#content', //内容容器 
				params: data,
				type: 'post',
				tempElem: '#tpl', //模块容器
				pageConfig: { //分页参数配置
					elem: '#paged', //分页容器
					pageSize: 10 //分页大小
				},
				success: function() { //渲染成功的回调
					//alert('渲染成功');
				},
				fail: function(msg) { //获取数据失败的回调
					//alert('获取数据失败')
				},
				complate: function() { //完成的回调
					//alert('处理完成');
					//重新渲染复选框
					form.render('checkbox');
					form.on('checkbox(allselector)', function(data) {
						var elem = data.elem;

						$('#content').children('tr').each(function() {
							var $that = $(this);
							//全选或反选
							$that.children('td').eq(0).children('input[type=checkbox]')[0].checked = elem.checked;
							form.render('checkbox');
						});
					});

					//绑定所有edit按钮事件						
					$('#content').children('tr').each(function() {
						var $that = $(this);
						$that.children('td:last-child').children('a[data-opt=edit]').on('click', function() {
							  var cid = $(this).data('name');
							  var RSSTypes = null;
							  
							  $.ajax({
									type: 'post',
									url:  'type/types',
									async: false,
									data: '',
									dataType: 'json',
									success: function(ret, status, xhr) {
										RSSTypes = ret.list;
								}
						     });

							if(RSSTypes !=null && RSSTypes.length>0){
								$.get('htmlTemp/selectType.html', null, function(form) {
									addBoxIndex = layer.open({
										type: 1,
										title: '选择类型',
										content: form,
										btn: ['确定', '取消'],
										shade: false,
										offset: ['100px', '30%'],
										area: ['300px', '200px'],
										zIndex: 19950924,
										yes: function(index) {
											 $.ajax({
													type: 'post',
													url:  'cha/Subscribe',
													async: false,
													data: {channel_id: cid, type_id: $('#type').val()},
													dataType: 'json',
													success: function(ret, status, xhr) {
														
														if(ret.isOk){
															layerTips.msg(ret.msg);
														}else{
															layerTips.msg(ret.msg);
														}
														layer.close(index);
												}
										     });
										},
										success: function(layero, index) {

											$(RSSTypes).each(function(i, item){

												$("#type").append('<option value="'+item.id+'">'+item.typename+'</option>');
												
												});
											
											var form = layui.form();
											form.render();
											
											}
										});
									});
											//弹出窗口成功后渲染表单
								
							}else{

								layer.msg("请先创建类别！");
							}
							  
							
							});
					});
				}
			});
			};
			loadtab(data);
			});
			
		</script>
#end

